<?php
namespace Ntxx\Controller;
use Think\Controller;
class LxController extends Controller {
    public function index(){
		//echo(U('index','','html',true));
		//dump($data);

        if(!in_app()){
            $this->display('answer/openapp');
            exit();
        }

        $count = M("topic")->where('type=0')->count();
		$this->assign("title",' - 训练模式');
		$this->assign("count",$count);
        $this->display();
	}
	
	public function getanswer(){
		$data = $this->answer();
		if(!$data){
			$this->ajaxReturn([
				"status"=>0,
				"info"=>"没有更多题目了",
			]);
			exit;
		}
		$this->ajaxReturn([
			"status"=>1,
			"info"=>$data,
		]);
		exit;
	}
	
	public function postanswer(){
		$i = I("post.i",0,"intval");
		$data = $this->answer();
		if(!$data){
			$this->ajaxReturn([
				"status"=>0,
				"info"=>"题目不存在",
			]);
			exit;
		}
		$this->ajaxReturn([
			"status"=>1,
			"info"=>$data["answer"],
		]);
		exit;
	}
	
	private function answer(){
		$index = I("post.index",0,"intval");
		if($index < 0){
			return false;
		}
		$data = M("topic")->where('type=0')->select();
		if(count($data)<=$index){
			return false;
		}
		return $data[$index];
	}
}